<?php
/**
 * Created by PhpStorm.
 * User: zhang-hong
 * Date: 2017/3/27
 * Time: 18:27
 */
class GetBatchInfoDataController extends Yaf_Controller_Abstract
{
    public function IndexAction()
    {

        $request = $this->getRequest();
        $user_department = $request->getPost("user_department","");
        $start_date = $request->getPost("start_date","");
        $end_date = $request->getPost("end_date","");
        $yiyuan_id = $request->getPost("yiyuan_id","");
        //医院id判断
        if($yiyuan_id === "")
        {
            Utility::apiJsonError(20028, '请输入医院ID');
        }
        elseif(!ctype_digit("$yiyuan_id"))
        {
            Utility::apiJsonError(20027, '医院ID必须是整型');
        }

        if($user_department == "")
        {
            Utility::apiJsonError(20064, '科室为空');
        }



        $where = array();
        if(empty($start_date)){
            $start_time = date('Y-m-d')." 00:00:00";
        }else{
            $start_time = $start_date." 00:00:00";
        }
        if(empty($end_date)){
            $end_time = date('Y-m-d')." 23:59:59";
        }else{
            $end_time = $end_date." 23:59:59";
        }
        $where['a.create_time'] = array(array('egt', $start_time),array('elt', $end_time));
        $where['a.state'] = array('EQ', 1);
        $where['b.user_department'] = array('EQ', $user_department);
        $batch_data = DB::select("a.*")
            ->distinct(TRUE)
            ->from(array("zhuyuan_tizheng_batch","a"))
            ->join(array("yiyuan_user","b"),"left")
            ->on("a.create_hushi_id","=","b.user_number")
            ->where("a.yiyuan_id","=",$yiyuan_id)
            ->and_where("a.create_time","between",array($start_time,$end_time))
            ->and_where("a.state","=",1)
            ->and_where("b.user_department","=",$user_department)
            ->order_by("a.create_time","DESC")
            ->execute()->as_array();
//        $batch_data = M('zhuyuan_tizheng_batch')->
//        join(' as a left join yiyuan_user as b on a.create_hushi_id=b.user_number')
//            ->where($where)
//            ->field('a.*')->distinct(true)->
//            order('a.create_time desc')->select();
        if(!empty($batch_data))
        {
            $data['state'] = '1';
        }
        else
        {
            $data['state'] = '0';
        }
        $data['error'] = 200;
        $data['msg'] = "";
        $data['result'] = $batch_data;
        Utility::apiJsonResultExtends($data);
    }
    
    /**
     * 更新体征数据
     */
    public function UpdateTizhengDataAction(){
    	$request = $this->getRequest();
    	$user_department = $request->getPost("user_department","");
    	if($user_department == "")
    	{
    		Utility::apiJsonError(20064, '科室为空');
    	}
    	
    	$data = $request->getPost("data","");
    	if(empty($data))
    	{
    		Utility::apiJsonError(20065, '更新数据为空');
    	}
    	$yiyuan_id = $request->getPost("yiyuan_id","");
    	//医院id判断
    	if($yiyuan_id === "")
    	{
    		Utility::apiJsonError(20028, '请输入医院ID');
    	}
    	elseif(!ctype_digit("$yiyuan_id"))
    	{
    		Utility::apiJsonError(20027, '医院ID必须是整型');
    	}
    	$compression_http_data = $request->getPost("compression_http_data", "off");
    	//解压缩数据
    	$data =  json_decode(Utility::uncompress($data,$compression_http_data),true);
    	if(!is_array($data)) Utility::apiJsonError(20066, '更新数据格式错误');
    	$model = new DataTizhengDataModel();
    	$result['state'] = $model->UpdateTizhengData($data) ? '1' : '0';
    	$result['error'] = '200';
    	$result['msg'] = '';

    	Utility::apiJsonResultExtends($result);
    }
}